// pages/returndetails/returndetails.js
import baseteam from '~/utils/teamleader.js'
import basesup from '~/utils/supplier.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        "orderbyid": '',

        addressjson: [],
        radio: ''
    },
    naveback() {
        setTimeout(() => {
            wx.navigateBack()
        }, 50);
    },
    naveaddress() {
        wx.navigateTo({
            url: '../returnaddress/returnaddress',
        })
    },
    onClose() {
        this.setData({
            express: false
        })
    },
    openexpress() {
        this.setData({
            express: true
        })
    },
    async ourdersdata() {
        let reqdata = {
            id: this.data.orderbyid
        }
        let resdata = await baseteam.orderbyid(reqdata)
        console.log(resdata)
        if (resdata.message == "操作成功") {
            this.setData({
                ourdersdetail: resdata.data.result
            })
            if (resdata.data.result.status == 1) {
                this.setData({
                    active: 1
                })
                return false
            }
            if (resdata.data.result.status == 2) {
                this.setData({
                    active: 2
                })
                return false
            }
            // ????
            // if (resdata.data.result.status == 5) {
            //     this.setData({
            //         active:4
            //     })
            //     return false
            // }
        }
    },
    async getaddress() {
        let resdata = await basesup.returnaddresslist()
        console.log(resdata)
        if (resdata.code == 200) {
            this.setData({
                addressjson: resdata.data.Result.records
            })
            for (let i = 0; i < resdata.data.Result.records.length; i++) {
                if (resdata.data.Result.records[i].isDefault == 1) {
                    this.setData({
                        radio: resdata.data.Result.records[i].id
                    })
                }
            }
        } else {
            setTimeout(() => {
                this.naveback()
            }, 2000);
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            orderbyid: options.orderbyid
        })
        this.ourdersdata()
        this.getaddress()
    },

    radioonChange(item) {
        console.log(item)
        this.setData({
            radio: item.detail
        })
    },
    afterRead(e) {
        console.log(e)
    },
    nextqrcode() {
        let that = this
        // 允许从相机和相册扫码
        wx.scanCode({
            // scanType:
            success(res) {
                console.log(res)
                that.setData({
                    value: res.result
                })
            }
        })
    },

    onConfirmexpress(items) {
        this.setData({
            expressselect: items.detail.index
        })
        this.onClose()
    },
    async bindsubmit() {
        let reqdata = {
            "id": this.data.ourdersdetail.id,
            "type": '2',
            "yesRecipients": "",
            "yesPhone": "",
            "yesProvince": "",
            "yesDetailedAddress": ""
        }
        for (const sele in this.data.addressjson) {
            if (this.data.addressjson[sele].id == this.data.radio) {
                reqdata.yesRecipients = this.data.addressjson[sele].recipients
                reqdata.yesPhone = this.data.addressjson[sele].phone
                reqdata.yesProvince = this.data.addressjson[sele].province
                reqdata.yesDetailedAddress = this.data.addressjson[sele].detailedAddress
            }
        }
        let resdata = await basesup.ourdertermination(reqdata)
        if (resdata.code == 200) {
            wx.showToast({
                title: '需要还样',
                icon: 'success',
                duration: 2000
            })
            setTimeout(() => {
                this.naveback()
            }, 1200);
        } else {
            wx.showToast({
                title: '操作失败，请重试',
                icon: 'error',
                duration: 2000
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.ourdersdata()
        this.getaddress()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})